package com.tudouji.d3.common;

import com.sun.jna.platform.win32.Sspi;
import com.tudouji.common.utils.DateUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.common.utils.sign.Md5Utils;
import com.tudouji.framework.config.D3Config;
import com.tudouji.framework.web.domain.server.Sys;
import com.tudouji.project.bill.domain.Sellform;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 签名工具类
 */
@Slf4j
public class SignUtil {

    /**
     * 编码参数
     * 除 appSecret 和 body外，将其余参数按 键名 升序排列拼接
     * sign作为传递签名值的参数，不参与签名。
     * 值为byte[] 类型的参数不参与签名
     */
    public String encodePARAMS(String methodAbs, LinkedHashMap<String, Object> queryParams, String jsonParamsBody, long timeStamp){
        String encodeStr = null;
        queryParams.put("appKey", D3Config.getAppKey());
        if (StringUtils.isNotEmpty(methodAbs)){
            queryParams.put("method", methodAbs);
        }
        queryParams.put("timestamp", timeStamp);
        List<Map.Entry<String, Object>> infoIds =new ArrayList<>(queryParams.entrySet());
        //排序
        Collections.sort(infoIds, (o1, o2) -> {
            String p1 =  o1.getKey();
            String p2 = o2.getKey();;
            return Integer.valueOf(p1)-Integer.valueOf(p2);
        });
        LinkedHashMap<String, Object> newMap = new LinkedHashMap <>();
        for(Map.Entry<String,Object> entity : infoIds){
            newMap.put(entity.getKey(), entity.getValue());
            log.info("encodePARAMS: " + entity.getKey());
        }
        for(Map.Entry<String,Object> entity : newMap.entrySet()){
            encodeStr += D3Config.getAppSecret() + entity.getKey() + entity.getValue();
        }
        encodeStr += jsonParamsBody;
        encodeStr += D3Config.getAppSecret();
        log.info("encodePARAMS before: " + encodeStr);
        encodeStr = Md5Utils.encryString(encodeStr);
        log.info("encodePARAMS after: " + encodeStr);
        return encodeStr;
    }

    /**
     * 验证签名是否正确
     * @param sign
     */
    public boolean equals(String sign, String methodAbs, LinkedHashMap<String, Object> queryParams, String jsonParamsBody, long timeStamp){
        String encodeStr = encodePARAMS(methodAbs, queryParams,jsonParamsBody,timeStamp);
        if (StringUtils.equals(sign, encodeStr)){
            return true;
        }
        return false;
    }

    /**
     * 编码地址
     */
    public void encodeURL(String url, String methodAbs, LinkedHashMap<String, Object> queryParams, String jsonParamsBody){
        long timeStamp = DateUtils.getNowDate().getTime();
        String encodeStr = encodePARAMS(methodAbs, queryParams,jsonParamsBody, timeStamp);
        url = url + "?appkey=" + D3Config.getAppKey() + "&method=" + methodAbs + "&sign=" + encodeStr + "&timestamp=" + timeStamp;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            get();
            System.out.println(i);
        }
    }

    public static void get(){
        if (true){
            return;
        }
        System.out.println("asd");
    }
}
